package com.treasure.mall.biz.repository.community;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.community.CommunityArticlePO;
import com.treasure.mall.zbq.common.ienum.community.CommunityEnums;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface CommunityArticleRepository extends JpaSearchableRepository<CommunityArticlePO, Long> {
    @Query("update CommunityArticlePO po set po.commentCount=po.commentCount+?2 where po.id=?1 and po.commentCount+?2>=0")
    @Modifying
    @Transactional
    void updateCommentCount(Long articleId, long count);

    @Query("update CommunityArticlePO po set po.likeCount=po.likeCount+?2 where po.id=?1 and po.likeCount+?2>=0")
    @Modifying
    @Transactional
    void updateLikeCount(Long articleId, long count);

    long countByAuthor_UserIdAndDeletedIsFalse(Long userId);


    long countByAuthor_UserIdAndAuditStatusAndDeletedIsFalse(Long userId, CommunityEnums.AuditStatus auditStatus);

    /**
     * 修改文章访问量
     *
     * @param articleId
     * @param count
     */
    @Query("update CommunityArticlePO po set po.visitCount=po.visitCount+?2 where po.id=?1 and po.visitCount+?2>=0")
    @Modifying
    @Transactional
    void updateVisitCount(Long articleId, long count);

    /**
     * 修改文章状态
     *
     * @param articleId
     * @param auditStatus
     * @param reason
     */
    @Query("update CommunityArticlePO po set po.auditStatus=?2 , po.reason=?3 where po.id=?1")
    @Modifying
    @Transactional
    void modifyCommunityArticleAuditStatus(Long articleId, CommunityEnums.AuditStatus auditStatus, String reason);


}
